<template>
  <el-container class="home-container">
    <!-- 顶部区域 -->
    <el-header>
      <div class="headerLogo">
        <!-- logo区域 -->
        <img src="../assets/logo.png" alt />
        <span>熊先森电商后台操作系统</span>
      </div>
      <!-- 退出按钮 -->
      <el-button class="outBtn"  plain @click="logout">
        <span>退出</span>
      </el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠按钮 -->
        <div id="folding" @click="foldingBtn">{{isCollapse ? '|||' : '&gt;&gt;&gt;|||&lt;&lt;&lt;'}}</div>
        <!-- 导航栏区域 -->
        <el-menu
          :unique-opened="true"
          background-color="#434A50"
          text-color="#fff"
          active-text-color="#409EFF"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级导航菜单 -->
          <el-submenu :index="item.id+'' " v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i id="iconfonts" :class="iconsList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级导航菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/'+subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main id="homeBgc">
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧栏数据
      menuList: [],
      // 一级导航栏字体图标
      iconsList: {
        '125': 'iconfont icon-icon-test1',
        '103': 'iconfont icon-icon-test',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-dingdan1',
        '145': 'iconfont icon-shuju1'
      },
      // 展开与折叠导航栏的初始状态
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    //   在created阶段请求侧边栏数据
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 点击按钮退出登陆
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 向服务器发送get请求 获取左侧导航栏数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 点击按钮切换菜单折叠与展开
    foldingBtn() {
      this.isCollapse = !this.isCollapse
    },
    // 点击二级菜单导航按钮
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #4370F5;
  display: flex;
  justify-content: space-between;
  align-items: center;
  img {
    width: 50px;
    height: 50px;
  }
  .headerLogo {
    display: flex;
    align-items: center;
    font-size: 20px;
    color: #fff;
    font-weight: 700;
    // 文字首行缩进
    text-indent: 20px;
  }
}
#iconfonts {
  padding-right: 10px;
  font-size: 18px;
}
.el-aside {
  background-color: #434a50;
}
.el-main {
  background-color: #fff;
}
#folding {
  // 文字之间的间距
  letter-spacing: 0.1em;
  //鼠标移入变为小手
  cursor: pointer;
  text-align: center;
  color: #fff;
  background-color: gray;
}
.el-menu {
  border-right: none;
}
.outBtn {
  width: 60px;
  height: 30px;
  padding-left: 15px;
  padding-top: 8px;
  background-color: #6484e6;
  opacity: 0.8;
  border: none;
  color: #fff;
}
</style>